<?php
	class Default_Model_Cart extends Zend_Db_Table_Abstract
	{
		protected $_name = 'cart';
		protected $_primary = 'id';
		protected $_db;
		
		public function __construct()
		{
			$this->_db = Zend_Registry::get('db');
		}
        
        public function getTableForType($type)
        {
            switch($type) {
                case    1   :   $name = 'fashion'; break;
                case    2   :   $name = 'baby'; break;
                case    3   :   $name = 'furniture'; break;
            }
            return $name;
        }
        
        public function saveItemCart($data)
        {
            $this->_db->insert($this->_name, $data);
            return $this->_db->lastInsertId();
        }
        
        public function saveItemCartInfo($data)
        {
            $this->_db->insert('cart_info', $data);
        }
        
        public function getProductByCartInfo($product_id, $type)
        {
            $data = array(
                'name',
                'price',
            );
            $table = $this->getTableForType($type);
            $sql = $this->_db->select()
                            ->from($table, $data)
                            ->where('id = ?', $product_id);
            $result = $this->_db->fetchRow($sql);
            return $result;
        }
        
        public function getProductForCat($product_id, $type)
        {
            $data = array(
                'id',
                'name',
                'price',
                'url',
                'type',
            );
            $table = $this->getTableForType($type);
            $sql = $this->_db->select()
                            ->from($table, $data)
                            ->where('id = ?', $product_id)
                            ->where('status = 1');
            $result = $this->_db->fetchRow($sql);
            return $result;
        }
        
        public function getProductName($product_id, $type)
        {
            $table = $this->getTableForType($type);
            $sql = $this->_db->select()
                            ->from($table, array('name'))
                            ->where('id = ?', $product_id)
                            ->where('status = 1');
            $result = $this->_db->fetchOne($sql);
            return $result;
    	}
        
    	public function getCost($product_id, $type)
        {
            $table = $this->getTableForType($type);
    		$sql = $this->_db->select()
                            ->from($table, array('price'))
                            ->where('id = ?', $product_id)
                            ->where('status = 1');
            $result = $this->_db->fetchOne($sql);
            return $result;
    	}
        
    	public function removeProduct($product_id, $type)
        {
    		$product_id = intval($product_id);
    		$max = count($_SESSION['cart']);
    		for($i = 0; $i < $max; $i++){
    			if(($product_id == $_SESSION['cart'][$i]['product_id']) && ($type == $_SESSION['cart'][$i]['type'])) {
    				unset($_SESSION['cart'][$i]);
    				break;
    			}
    		}
    		$_SESSION['cart'] = array_values($_SESSION['cart']);
    	}
        
        public function getOrderTotal()
        {
    		$max = count($_SESSION['cart']);
    		$sum = 0;
    		for($i = 0; $i < $max; $i++){
    			$product_id = $_SESSION['cart'][$i]['product_id'];
                $type = $_SESSION['cart'][$i]['type'];
    			$quantity = $_SESSION['cart'][$i]['quantity'];
    			$cost = $this->getCost($product_id, $type);
    			$sum += $cost * $quantity;
    		}
    		return $sum;
    	}
        
        public function addToCart($product_id, $quantity, $type)
        {
    		if(($product_id < 1) or ($quantity < 1)) return;
            
            if(!is_numeric($product_id)) return;
    		
    		if(is_array($_SESSION['cart'])) {
    			if($this->checkProductExists($product_id, $type)) return;
    			$max = count($_SESSION['cart']);
    			$_SESSION['cart'][$max]['product_id'] = $product_id;
                $_SESSION['cart'][$max]['type'] = $type;
    			$_SESSION['cart'][$max]['quantity'] = $quantity;
    		}
    		else{
    			$_SESSION['cart'] = array();
    			$_SESSION['cart'][0]['product_id'] = $product_id;
                $_SESSION['cart'][0]['type'] = $type;
    			$_SESSION['cart'][0]['quantity'] = $quantity;
    		}
    	}
        
        public function checkProductExists($product_id, $type)
        {
    		$product_id = intval($product_id);
    		$max = count($_SESSION['cart']);
    		$flag = 0;
    		for($i = 0; $i < $max; $i++) {
    			if(($product_id == $_SESSION['cart'][$i]['product_id']) && ($type == $_SESSION['cart'][$i]['type'])) {
    				$flag = 1;
    				break;
    			}
    		}
    		return $flag;
    	}
        
        public function getCartInfo($cart_id)
        {
            $sql = $this->_db->select()
                            ->from('cart_info')
                            ->where('cart_id = ?', $cart_id)
                            ->order('id ASC');
            $result = $this->_db->fetchAll($sql);
            return $result;
        }
    }
